package com.neusoft.neuiotms.dcam.fixed.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.dto.StockDTO;
import com.neusoft.neuiotms.dcam.fixed.service.StockService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "库存管理")
@RestController
@RequestMapping(value = "/api/w/fixed/stock")
public class StockController {

    @Autowired
    private StockService stockService;

    @ApiOperation("查询库存")
    @GetMapping
    public ResultListDTO<StockDTO> index() {
        final List<StockDTO> StockDTOs = this.stockService.findAll();
        return ResultListDTO.success(StockDTOs);
    }

    @ApiOperation("创建库存")
    @PostMapping
    public ResultDTO<StockDTO> create(@RequestBody final StockDTO dto) {
        final StockDTO result = this.stockService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("库存详情")
    @GetMapping("/{id}")
    public ResultDTO<StockDTO> details(@PathVariable final Long id) {
        final StockDTO dto = this.stockService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除库存")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.stockService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改库存")
    @PutMapping("/{id}")
    public ResultDTO<StockDTO> update(@PathVariable final Long id, @RequestBody final StockDTO dto) {
        final StockDTO result = this.stockService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("分页查询库存")
    @GetMapping("/p")
    public PageResultDTO<StockDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.stockService.search(pageable, searchable));
    }

    @ApiOperation("导出库存")
    @GetMapping(value = "/export")
    public void download(final Searchable searchable, final Pageable pageable,
            final HttpServletResponse response) {

        final Workbook workbook = this.stockService.export(searchable, pageable);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("库存导出" + LocalDateTime.now() + ".xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            StockController.log.error("库存导出失败", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                StockController.log.error("库存货品导出关闭流失败", e);
            }
        }

    }

    @ApiOperation("导出库存查询结果")
    @GetMapping(value = "/sqexport")
    public void downloadStockQuery(final Searchable searchable, final Pageable pageable,
            final HttpServletResponse response) {

        final Workbook workbook = this.stockService.downloadStockQuery(searchable, pageable);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("库存查询结果" + LocalDateTime.now() + ".xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            StockController.log.error("库存查询结果导出失败", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                StockController.log.error("库存查询结果导出关闭流失败", e);
            }
        }

    }
}
